package com.jd.controller;

import com.jd.bo.AddressBO;
import com.jd.bo.ShopcartBO;
import com.jd.pojo.UserAddress;
import com.jd.service.AddressService;
import com.jd.service.ItemService;
import com.jd.utils.JDJSONResult;
import com.jd.utils.MobileEmailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Api(value = "地址相关",tags = {"地址相关的api接口"})
@RestController
@RequestMapping("address")
public class AddressController extends BaseController {

    @Autowired
    private AddressService addressService;


    /**
     * 用户在确认订单页面，可以针对收货地址做如下操作
     * 1、查询用户的所有收货地址列表
     * 2、新增收货地址
     * 3、删除收货地址
     * 4、修改收货地址
     * 5、设置默认地址
     */
    @ApiOperation(value = "根据用户id查询收货地址列表",notes = "根据用户id查询收货地址列表",httpMethod = "POST")
    @PostMapping("/list")
    public JDJSONResult list(@ApiParam(name="itemId",value = "用户id",required = true)
                                 @RequestParam String userId){
        if(StringUtils.isBlank(userId)){
            return JDJSONResult.errorMsg("");
        }
        List<UserAddress> list= addressService.queryAll(userId);
        return JDJSONResult.ok(list);
    }



    @ApiOperation(value = "用户新增地址",notes = "用户新增地址",httpMethod = "POST")
    @PostMapping("/add")
    public JDJSONResult add(@RequestBody AddressBO addressBO){
        JDJSONResult checkRes=checkAddress(addressBO);
        if(checkRes.getStatus()!=200){
            return checkRes;
        }
        addressService.addNewUserAddress(addressBO);
        return JDJSONResult.ok();
    }

    @ApiOperation(value = "用户修改地址",notes = "用户修改地址",httpMethod = "POST")
    @PostMapping("/update")
    public JDJSONResult update(@RequestBody AddressBO addressBO){
        if(StringUtils.isBlank(addressBO.getAddressId())){
            return JDJSONResult.errorMsg("修改地址错误：addressId不能为空");
        }

        JDJSONResult checkRes=checkAddress(addressBO);
        if(checkRes.getStatus()!=200){
            return checkRes;
        }
        addressService.updateUserAddress(addressBO);
        return JDJSONResult.ok();
    }

    @ApiOperation(value = "用户删除地址",notes = "用户删除地址",httpMethod = "POST")
    @PostMapping("/delete")
    public JDJSONResult delete(@RequestParam String userId,
                               @RequestParam String addressId){
        if(StringUtils.isBlank(userId)||StringUtils.isBlank(addressId)){
            return JDJSONResult.errorMsg("");
        }
        addressService.deleteUserAddress(userId,addressId);
        return JDJSONResult.ok();
    }


    @ApiOperation(value = "用户设置默认地址",notes = "用户设置默认地址",httpMethod = "POST")
    @PostMapping("/setDefalut")
    public JDJSONResult setDefalut(@RequestParam String userId,
                               @RequestParam String addressId){
        if(StringUtils.isBlank(userId)||StringUtils.isBlank(addressId)){
            return JDJSONResult.errorMsg("");
        }
        addressService.updateUserAddressToBeDefault(userId,addressId);
        return JDJSONResult.ok();
    }


    private JDJSONResult  checkAddress(AddressBO addressBO){
        String receiver=addressBO.getReceiver();
        if(StringUtils.isBlank(receiver)){
            return JDJSONResult.errorMsg("收货人不能为空");
        }
        if(receiver.length()>12){
            return JDJSONResult.errorMsg("收货人姓名不能太长");
        }
        String mobile=addressBO.getMobile();
        if(StringUtils.isBlank(mobile)){
            return JDJSONResult.errorMsg("收货人手机号不能为空");
        }
        if(mobile.length()!=11){
            return JDJSONResult.errorMsg("收货人手机号长度不正确");
        }
        boolean isMobileOk= MobileEmailUtils.checkMobileIsOk(mobile);
        if(!isMobileOk){
            return JDJSONResult.errorMsg("收货人手机号格式不正确");
        }
        String province=addressBO.getProvince();
        String city=addressBO.getCity();
        String district=addressBO.getDistrict();
        String detail=addressBO.getDetail();
        if(StringUtils.isBlank(province)||
                StringUtils.isBlank(city)||
                StringUtils.isBlank(district)||
                StringUtils.isBlank(detail)){
            return JDJSONResult.errorMsg("收货地址信息不能为空");
        }
        return JDJSONResult.ok();
    }



}
